/*
  Copyright (c) 2005-2011, Dominik Reichl <dominik.reichl@t-online.de>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  - Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer. 
  - Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
  - Neither the name of ReichlSoft nor the names of its contributors may be
    used to endorse or promote products derived from this software without
    specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

/////////////////////////////////////////////////////////////////////////////
// For version history see the source file.


#pragma once
#include <wtl/atlctrls.h>

/////////////////////////////////////////////////////////////////////////////

#ifndef TCH_STDPWCHAR
#define TCH_STDPWCHAR _T('*')
#endif

#ifndef SE_XORPAD_SIZE
#define SE_XORPAD_SIZE 32
#endif

//class CSecureDropTarget;

class CSecureEditEx : public CWindowImpl<CSecureEditEx, CEdit>
{
	typedef CWindowImpl<CSecureEditEx, CEdit> _super;
// Construction
public:
	CSecureEditEx();
	virtual ~CSecureEditEx();

	void InitEx();

	void EnableSecureMode(BOOL bEnable = TRUE);
	BOOL IsSecureModeEnabled();

	// Retrieve the currently entered password
	LPTSTR GetPassword();

	// Securely free the passwords returned by GetPassword()
	static void DeletePassword(LPTSTR lpPassword);

	// Set the currently entered password, may be NULL
	void SetPassword(LPCTSTR lpPassword);

	//{{AFX_VIRTUAL(CSecureEditEx)
	//}}AFX_VIRTUAL

private:
	void _DeleteAll();
	void _ClearSelection();
	void _InsertCharacters(unsigned int uPos, LPCTSTR lpSource, unsigned int uNumChars);
	void _DeleteCharacters(unsigned int uPos, unsigned int uCount);
	void _EncryptBuffer(BOOL bEncrypt = TRUE);

	BOOL _RegisterDropTarget();
	void _Paste(LPCTSTR lpSource);

	BOOL m_bSecMode;
	LPTSTR m_pXorPad;
	CAtlArray<LPTSTR> m_apChars;
	int m_nOldLen;

	//CSecureDropTarget* m_pSecDrop;

public:
	static LPTSTR AllocMemory(size_t uCount);
	static void SetMemoryEx(void *pDest, int c, size_t uCount);
	static void DeleteTPtr(LPTSTR lp, BOOL bIsArray, BOOL bIsString);
public:
	BEGIN_MSG_MAP(CSecureEditEx)
		REFLECTED_COMMAND_CODE_HANDLER_EX(EN_UPDATE, OnEnUpdate)
		MSG_WM_KEYDOWN(OnKeyDown)
		MSG_WM_KEYUP(OnKeyUp)
		//MSG_WM_CHAR(OnChar)
		//MSG_WM_DEADCHAR(OnDeadChar)
		//MSG_WM_MOUSEMOVE(OnMouseMove)
		//MSG_WM_LBUTTONDBLCLK(OnLButtonDblClk)
		//MSG_WM_LBUTTONUP(OnLButtonUp)	
		//MSG_WM_MBUTTONDBLCLK(OnMButtonDblClk)
		//MSG_WM_MBUTTONUP(OnMButtonUp)

		//MSG_WM_RBUTTONUP(OnRButtonUp)
		//MSG_WM_RBUTTONDBLCLK(OnRButtonDblClk)
		
		REFLECTED_COMMAND_CODE_HANDLER_EX(EN_SETFOCUS, OnEnSetFocus)
		DEFAULT_REFLECTION_HANDLER()
	END_MSG_MAP()


protected:

	LRESULT OnEnUpdate(WORD wNotifyCode, WORD wID, HWND hWndCtl);
	LRESULT OnEnSetFocus(WORD wNotifyCode, WORD wID, HWND hWndCtl);
	LRESULT OnLButtonDblClk(UINT Flags, CPoint Pt);
	LRESULT OnLButtonUp(UINT Flags, CPoint Pt);
	LRESULT OnMButtonUp(UINT Flags, CPoint Pt);
	LRESULT OnMouseMove(UINT Flags, CPoint Pt);
	LRESULT OnRButtonUp(UINT Flags, CPoint Pt);
	LRESULT OnRButtonDblClk(UINT Flags, CPoint Pt);
	LRESULT OnMButtonDblClk(UINT Flags, CPoint Pt);
	LRESULT OnDeadChar(TCHAR ch, UINT cRepeat, UINT flags);
	LRESULT OnKeyDown(TCHAR nChar, UINT cRepeat, UINT flags);
	LRESULT OnKeyUp(TCHAR vk, UINT cRepeat, UINT flags);
	LRESULT OnChar(TCHAR ch, UINT cRepeat, UINT flags);
};

/////////////////////////////////////////////////////////////////////////////
//
//class CSecureDropTarget : public COleDropTarget
//{
//public:
//	CSecureDropTarget(CSecureEditEx* pControl);
//
//private:
//	CSecureEditEx* m_pControl;
//
//protected:
//	// Implement COleDropTarget
//	virtual DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject,
//		DWORD dwKeyState, CPoint point);
//	virtual DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject,
//		DWORD dwKeyState, CPoint point);
//	virtual BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject,
//		DROPEFFECT dropEffect, CPoint point);
//};
//
//class ATL_NO_VTABLE CNSFDropTarget :
//    public CComObjectRootEx<CComSingleThreadModel>,
//    public IDropTarget
//{
//public:
//
//DECLARE_PROTECT_FINAL_CONSTRUCT()
//
//BEGIN_COM_MAP(CNSFDropTarget)
//    COM_INTERFACE_ENTRY_IID(IID_IDropTarget,IDropTarget)
//END_COM_MAP()
//
//public:
//	CNSFDropTarget(CSecureEditEx* pControl);
//    //Data members
//    CSecureEditEx* m_pControl;
//    UINT m_iAcceptFmt;    
//    LPITEMIDLIST m_pidl;  //simple PIDL relative to parent folder
//   // CNWSPidlMgr  m_PidlMgr;
//
//
//    HRESULT FinalConstruct()
//    { 
//          m_pFolder = NULL;
//          m_iAcceptFmt = 0;
//
//          return S_OK;
//    }
//
//    void FinalRelease()
//    {
//		ATLASSERT(m_pFolder);
//		if(m_pFolder==NULL)
//		{
//			MessageBox(NULL,_T("CDropTarget::FinalRelease() m_pFolder==NULL"),_T("NSF"),MB_OK);
//			return;
//		}
//        m_pFolder->Release();
//
//        if( m_pidl != NULL)
//		{
//            m_PidlMgr.Delete(m_pidl);
//			m_pidl=NULL;
//		}
//
//		m_iAcceptFmt = 0;
//    }
//
//  //  HRESULT _Init(CMyVirtualFolder *pFolder,/* HWND hWnd,*/LPCITEMIDLIST pidl)
//  //  {
//  //      ATLTRACE(_T("CDropTarget::_Init\n"));
//  //      ATLASSERT(pFolder);
//		//if(pFolder==NULL)
//		//{
//		//	MessageBox(NULL,_T("CDropTarget::_Init() pFolder==NULL"),_T("NSF"),MB_OK);
//		//	return E_FAIL;
//		//}
//  //      m_pFolder = pFolder;
//  //      m_pFolder->AddRef();
//
//  //      m_pidl=m_PidlMgr.Copy(pidl);
//  //      return S_OK;
//  //  }
//
//    // Implementation
//	DWORD _QueryDrop()
//    {
//        if( 0 == m_iAcceptFmt ) 
//             return DROPEFFECT_NONE;
//
//		if( FMT_HDROP_INDEX == m_iAcceptFmt)
//			return DROPEFFECT_COPY;
//
//		if( FMT_NSEDRAGDROP_INDEX == m_iAcceptFmt)
//			return DROPEFFECT_MOVE;
//
//        return DROPEFFECT_NONE;
//    }
//
//    // IDropTarget
//
//    STDMETHOD(DragEnter)(LPDATAOBJECT pDataObj, 
//                                DWORD dwKeyState, 
//                                POINTL, 
//                                LPDWORD pdwEffect)
//    {
//        ATLTRACE(_T("CDropTarget::DragEnter\n"));
//        ATLASSERT(pDataObj);
//
//        // Does the drag source provide self-defined CF types?
//        m_iAcceptFmt = 0;
//
//        if( m_iAcceptFmt == 0 ) 
//        {
//			FORMATETC fe2 = { _Module.m_CFSTR_NSEDRAGDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
//			if( S_OK == pDataObj->QueryGetData(&fe2) )
//			{
//				m_iAcceptFmt = FMT_NSEDRAGDROP_INDEX;
//			}
//			else
//			{
//				FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
//				if( S_OK == pDataObj->QueryGetData(&fe) )
//				{
//					m_iAcceptFmt = FMT_HDROP_INDEX;
//				}
//			}
//        }
//		*pdwEffect = _QueryDrop();
//        return S_OK;
//    }
//
//    STDMETHOD(DragOver)(DWORD dwKeyState, POINTL /*pt*/, LPDWORD pdwEffect)
//    {
//		*pdwEffect = _QueryDrop();
//        return S_OK;
//    }
//    
//    STDMETHOD(DragLeave)(VOID)
//    {
//        ATLTRACE(_T("CDropTarget::DragLeave\n"));
//        m_iAcceptFmt = 0;
//        return S_OK;
//    }
//
//    STDMETHOD(Drop)(LPDATAOBJECT pDataObj,
//                          DWORD dwKeyState,
//                          POINTL /*pt*/,
//                          LPDWORD pdwEffect)
//    {
//        ATLTRACE(_T("CDropTarget::Drop\n"));
//        ATLASSERT(pDataObj);
//        ATLASSERT(pdwEffect);
//        ATLASSERT(m_pFolder);
//
//        *pdwEffect = DROPEFFECT_NONE; // default to failed/cancelled
//
//        // Determine drop effect...
//		DWORD dwDropEffect = _QueryDrop();
//
//        // Did we accept this drop effect?
//        if( dwDropEffect == DROPEFFECT_NONE ) 
//			return S_OK;
//
//        // Drop data...
//        HRESULT Hr;
//        if( SUCCEEDED(Hr = m_pFolder->_DoDrop(pDataObj, dwDropEffect,m_pidl,m_iAcceptFmt) ) ) 
//		{
//			*pdwEffect = dwDropEffect;
//            return  S_OK;
//        }
//		else
//		{
//			return Hr;
//		}
//    }
//};
//
